package Concurrency1.Chapter01_Base;

import java.util.concurrent.*;

/**
 * 功能说明：启动线程的方式
 * 方式1: start
 * 方式2: 提交给线程池管理
 * 开发人员：@author MaLi
 */
public class C02_RunThread {
    public static void main(String[] args) {
        //Mode1: start开启线程
        new Thread(() -> {
            //输出自定义线程的属性信息
            showThreadInfo("SubThread");
        }, "SomeThread").start();
        // 输出主线程的线程属性信息
        showThreadInfo("MainThread");

        // Mode2: 提交任务给线程池管理
        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        //提交任务给线程池
        executor.submit(()-> System.out.println("通过线程池管理的Task"));
        executor.shutdown();
    }

    /**
     * 输出线程的状态信息
     *
     * @param flag 如果是MainThread, 就使用system.err输出, 以示区别
     */
    private static void showThreadInfo(String flag) {
        Thread currentThread = Thread.currentThread();
        if ("MainThread".equalsIgnoreCase(flag)) {
            System.err.println("[" + flag + "] Thread Name: " + currentThread.getName());
            System.err.println("[" + flag + "] Thread State: " + currentThread.getState());
            System.err.println("[" + flag + "] Thread Priority: " + currentThread.getPriority());
            System.err.println("[" + flag + "] Thread ID: " + currentThread.getId());
            System.err.println("[" + flag + "] Thread isDaemon: " + currentThread.isDaemon());
            System.err.println("[" + flag + "] Thread isAlive: " + currentThread.isAlive());
        } else {
            System.out.println("[" + flag + "] Thread Name: " + currentThread.getName());
            System.out.println("[" + flag + "] Thread State: " + currentThread.getState());
            System.out.println("[" + flag + "] Thread Priority: " + currentThread.getPriority());
            System.out.println("[" + flag + "] Thread ID: " + currentThread.getId());
            System.out.println("[" + flag + "] Thread isDaemon: " + currentThread.isDaemon());
            System.out.println("[" + flag + "] Thread isAlive: " + currentThread.isAlive());
        }
    }
}
